using Microsoft.Office.Interop.Excel;
using System;
using System.Data;
using System.Drawing;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationTypeSubtotal
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private Range range;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationTypeSubtotal(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string filename)
		{
			if (filename == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Add(Type.Missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeDataToExcel(string filename, DataSet ds)
		{
			this.OpenExcel(filename);
			this.MakeDataToExcel_1(ds, filename);
		}

		public void MakeDataToExcel_2(string filename, DataSet ds)
		{
			this.OpenExcel(filename);
			this.MakeDataToExcel_2(ds, filename);
		}

		private void MakeDataToExcel_1(DataSet ds, string filename)
		{
			int num = 1;
			try
			{
				for (int i = 0; i < ds.Tables.Count; i++)
				{
					for (int j = 0; j < ds.Tables[i].Columns.Count; j++)
					{
						this.workSheet.Cells[num, j + 1]= ds.Tables[i].Columns[j].ColumnName;
					}
					num++;
					for (int k = 0; k < ds.Tables[i].Rows.Count; k++)
					{
						for (int j = 0; j < ds.Tables[i].Columns.Count; j++)
						{
							this.workSheet.Cells[num, j + 1]= ds.Tables[i].Rows[k][j];
						}
						num++;
					}
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.SaveAs(filename, this.missing, this.missing, this.missing, this.missing, this.missing, XlSaveAsAccessMode.xlExclusive, this.missing, this.missing, this.missing, this.missing, this.missing);
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		private void MakeDataToExcel_2(DataSet ds, string filename)
		{
			int num = 1;
			try
			{
				for (int i = 0; i < ds.Tables.Count; i++)
				{
					for (int j = 0; j < ds.Tables[i].Columns.Count; j++)
					{
						this.workSheet.Cells[num, j + 1]= ds.Tables[i].Columns[j].ColumnName;
					}
					num++;
					for (int k = 0; k < ds.Tables[i].Rows.Count; k++)
					{
						if (Convert.ToInt32(ds.Tables[i].Rows[k]["总数量"]) != Convert.ToInt32(ds.Tables[i].Rows[k]["已发货"]))
						{
							for (int j = 0; j < ds.Tables[i].Columns.Count; j++)
							{
								this.workSheet.Cells[num, j + 1]= ds.Tables[i].Rows[k][j];
							}
							this.range = this.workSheet.get_Range(this.workSheet.Cells[num, 1], this.workSheet.Cells[num, ds.Tables[i].Columns.Count]);
							if (Convert.ToInt32(ds.Tables[i].Rows[k]["总数量"]) == Convert.ToInt32(ds.Tables[i].Rows[k]["已发货"]))
							{
								this.range.Interior.Color=(Color.FromArgb(255, 124, 252, 0).ToArgb());
							}
							if (Convert.ToInt32(ds.Tables[i].Rows[k]["总数量"]) < Convert.ToInt32(ds.Tables[i].Rows[k]["已发货"]))
							{
								this.range.Interior.Color=(Color.FromArgb(255, 255, 0, 0).ToArgb());
							}
							if (Convert.ToInt32(ds.Tables[i].Rows[k]["总数量"]) > Convert.ToInt32(ds.Tables[i].Rows[k]["已发货"]) && Convert.ToInt32(ds.Tables[i].Rows[k]["已发货"]) > 0)
							{
								this.range.Interior.Color=(Color.Turquoise.ToArgb());
							}
							num++;
						}
					}
					num++;
				}
				this.workSheet.Columns.AutoFit();
				this.workBook.SaveAs(filename, this.missing, this.missing, this.missing, this.missing, this.missing, XlSaveAsAccessMode.xlExclusive, this.missing, this.missing, this.missing, this.missing, this.missing);
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
